package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.ProjectService;
import hu.bme.aut.consulttime.client.ProjectServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;
import hu.bme.aut.consulttime.enums.ProjectType;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class CreateProject extends DialogBox {

	/**
	 * Create a remote service proxy to talk to the server-side service.
	 */
	private ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);

	private TextBox textBoxTitle;
	private ListBox comboBoxType;
	private TextArea textAreaDescription;
	private ListBox comboBoxTutors;
	
	//Tutorlist 
	List<UserDTO> tutorlist;

	public CreateProject() {
		super(true);

		FlexTable flexTable = new FlexTable();

		Label lblNewLabel = new Label("Create New Project");
		flexTable.setWidget(0, 0, lblNewLabel);

		Label lblProjectTitle = new Label("Project Title");
		flexTable.setWidget(1, 0, lblProjectTitle);

		textBoxTitle = new TextBox();
		flexTable.setWidget(1, 1, textBoxTitle);
		textBoxTitle.setWidth("100%");
		// --------------------- Upload the Types ---------------------
		Label lblProjectType = new Label("Project Type");
		flexTable.setWidget(2, 0, lblProjectType);

		
		comboBoxType = new ListBox();
		flexTable.setWidget(2, 1, comboBoxType);
		comboBoxType.setWidth("100%");
		ProjectType[] values = ProjectType.values();
		for (int i = 0; i < values.length; ++i) {
			comboBoxType.addItem(values[i].toString(), values[i].name());
		}
		// --------------------- Upload the Tutors ---------------------
		Label lblTutors = new Label("Tutors");
		flexTable.setWidget(3, 0, lblTutors);

		comboBoxTutors = new ListBox(true);
		flexTable.setWidget(3, 1, comboBoxTutors);
		comboBoxTutors.setWidth("100%");

		projectService.retrieveTutors(new AsyncCallback<List<UserDTO>>() {
			@Override
			public void onSuccess(List<UserDTO> result) {
				tutorlist = result;
				for(UserDTO tmp : result){
					comboBoxTutors.addItem(tmp.getFirstName() + " " + tmp.getLastName() + " (" + tmp.getUserName() + ")", String.valueOf(tmp.getUserId()));
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}
		});

		Label lblDescription = new Label("Description");
		flexTable.setWidget(4, 0, lblDescription);

		textAreaDescription = new TextArea();
		flexTable.setWidget(4, 1, textAreaDescription);
		textAreaDescription.setSize("305px", "113px");
		
		Button btnCreate = new Button("Create");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ProjectType pt = ProjectType.valueOf(comboBoxType.getValue(comboBoxType.getSelectedIndex()));
				ProjectDTO projectDTO = new ProjectDTO(textBoxTitle.getText(), pt, textAreaDescription.getText());
				
				//Get the selected tutors
				List<String> selectedTutorStringList = new ArrayList<String>();
				for(int i=0; i<comboBoxTutors.getItemCount(); ++i ){
					if(comboBoxTutors.isItemSelected(i)){
						selectedTutorStringList.add(comboBoxTutors.getValue(i));
					}
				}
				
				//Get tutorID-s
				List<UserDTO> selectedTutorList = new ArrayList<UserDTO>();
				for(UserDTO tmp : tutorlist){
					if(selectedTutorStringList.contains(String.valueOf(tmp.getUserId()))){
						selectedTutorList.add(tmp);
					}
				}
				projectDTO.setTutors(selectedTutorList);
				
				projectService.createProject(projectDTO, new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if(result == true){
							Window.confirm("'"+textBoxTitle.getText() + "' project created!");
					        //Refresh the table
					        ProjectPage.loadProjects();
							
						}else{
							Window.alert("Project creation failed!");
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Remote Procedure Call - Failure");
					}
				});
			}
		});
		flexTable.setWidget(5, 1, btnCreate);
		
		setWidget(flexTable);

	}
}
